home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 6.7 KB | 320 lines | [TEXT/MPS ] |
- /*
- File: DebugStream.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #pragma trace off
-
- #ifndef __BOVINESERVER__
- #include "BovineServer.h"
- #endif
-
- #ifndef __DEBUGSTREAM__
- #include "DebugStream.h"
- #endif
-
- #ifndef __EVENTS__
- #include "Events.h"
- #endif
-
- #ifndef __IOSTREAM__
- #include <IOStream.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __TRANSCRIPTWINDOW__
- #include "TranscriptWindow.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- /***********************************|****************************************/
-
- #pragma segment DebugStream
-
- /***********************************|****************************************/
-
- extern Boolean gBovineServerWasInitialized;
-
-
- #define newWindow 1
-
- TTranscriptWindow* gTranscriptWindow = nil;
-
-
-
- /***********************************|****************************************/
-
- debugstream::debugstream (streambuf *buf) :
- ostream(buf),
- ios ( buf )
- {
- fEncodedCharacterOutput = false;
- }
-
- /***********************************|****************************************/
-
- debugstream::~debugstream ()
- {
- }
-
- /***********************************|****************************************/
-
- void debugstream::SetEncodedOutput (Boolean newValue)
- {
- fEncodedCharacterOutput = newValue;
- }
-
- /***********************************|****************************************/
-
- debugstream& debugstream::write(register const char* s, register int n)
- {
- if (fEncodedCharacterOutput)
- {
- for (register int i = 0; i < n; ++i)
- {
- register unsigned char c = *(s + i);
-
- if (c < 0x80)
- {
- switch (c)
- {
- case 0: ostream::operator << ( "^@" ); break;
- case 9: ostream::operator << ( "<tab>" ); break;
- case 10: ostream::operator << ( "<lf>" ); break;
- case 13: ostream::operator << ( "<cr>" ); break;
- case 27: ostream::operator << ( "<esc>" ); break;
- default: *this << c; break;
- }
- }
- else
- {
- *this << "<" << hexo << (int) c << deco << ">";
- }
- }
-
- fEncodedCharacterOutput = false;
- }
- else
- {
- ostream::write ( s, n );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TWWStreamBuf::TWWStreamBuf ()
- {
- const long wwstreambufSize = 80;
-
- // Create a buffer to store data into.
- char *buffer = (char*) ::NewPtrClear(wwstreambufSize);
- setbuf (buffer, (int) wwstreambufSize);
-
- // Give the entire buffer (save one character) to the put area
- setp (base(), ebuf()-1);
- }
-
- /***********************************|****************************************/
-
- TWWStreamBuf::~TWWStreamBuf ()
- {
- gBovineServerWasInitialized = false; // this is really a “debug streaming is doable” flag
-
- // Dispose of the memory we allocated to store the buffer
- if ( base())
- DisposePtr( (Ptr) base());
- }
-
- /***********************************|****************************************/
-
- short GetCurrentModifiers()
- {
- EventRecord event;
- (void) OSEventAvail(0, &event);
- return event.modifiers;
- }
-
- /***********************************|****************************************/
-
- inline Boolean ShiftDown(short modifiers)
- { return (modifiers & shiftKey) != 0; }
-
- /***********************************|****************************************/
-
- inline Boolean CommandDown(short modifiers)
- { return (modifiers & cmdKey) != 0; }
-
- /***********************************|****************************************/
-
- inline Boolean OptionDown(short modifiers)
- { return (modifiers & optionKey) != 0; }
-
- /***********************************|****************************************/
-
- Boolean ShiftCurrentlyDown()
- {
- return ShiftDown(GetCurrentModifiers());
- }
-
- /***********************************|****************************************/
-
- Boolean OptionCurrentlyDown()
- {
- return OptionDown(GetCurrentModifiers());
- }
-
- /***********************************|****************************************/
-
- Boolean CommandCurrentlyDown()
- {
- return CommandDown(GetCurrentModifiers());
- }
-
- /***********************************|****************************************/
-
- int TWWStreamBuf::overflow (int c)
- {
- // Make sure we've got a buffer to dump stuff into.
- if (allocate() == EOF)
- return (EOF);
- else if ( base() == nil)
- return (EOF);
-
- // If the user holds down both command and option, then don't show
- // any output -- just immediately return.
- Boolean skipOutput = (CommandCurrentlyDown() && OptionCurrentlyDown());
- if (skipOutput) {
- setp (base(), ebuf()-1);
- return 0;
- }
-
- #ifdef THINK_CPLUS
- // In Think, '/n' is not carriage return. So, change it.
- if ( (c == '\r') || ( c == '\n') )
- c = 13;
- #endif
-
- // Put the characters in the existing buffer into the debugging stream
- long len = pptr() - pbase();
- if (c == EOF)
- {
- #if newWindow
- if ( gTranscriptWindow )
- gTranscriptWindow->Append ( pbase(), len );
- #else
- WWAddText (pbase(), len);
- WWFlushOutputFile();
- #endif
- } else {
- *pptr() = c;
-
- #if newWindow
- if ( gTranscriptWindow )
- {
- gTranscriptWindow->Append ( pbase(), len + 1 );
- gTranscriptWindow->FlushOutputFile ();
- }
- #else
- WWAddText (pbase(), len + 1);
-
- if ( keithFlag.Flag(23))
- WWFlushOutputFile();
- #endif
- }
-
- if (ShiftCurrentlyDown() && ( IsGatewayPausing() == false ))
- TYield();
-
- // Reset this buffer to the entire buffer space
- setp (base(), ebuf()-1);
-
- return (0);
- }
-
- /***********************************|****************************************/
-
- int TWWStreamBuf::underflow ()
- {
- // We don't support input in this stream.
- return (EOF);
- }
-
- /***********************************|****************************************/
-
- #if 1
- int TWWStreamBuf::sync ( )
- {
- overflow( EOF );
- return 0;
- }
- #endif
-
- /***********************************|****************************************/
-
- // put the date (short format) into the output stream
- ostream& date (ostream& o)
- {
- Str255 theDate;
- unsigned long now;
-
- GetDateTime (&now);
- IUDateString (now, longDate, theDate);
-
- o.write (&theDate[1], theDate[0]);
- o.put (' ');
-
- return o;
- }
-
- /***********************************|****************************************/
-
- // put the time into the output stream
- ostream& time (ostream& o)
- {
- Str255 theTime;
- unsigned long now;
-
- GetDateTime (&now);
- IUTimeString (now, true, theTime);
-
- o.write (&theTime[1], theTime[0]);
- o.put (' ');
-
- return o;
- }
-
- /***********************************|****************************************/
-
- // makes the next item output in an encoded character format
- debugstream& encoded (debugstream& s)
- {
- s.SetEncodedOutput (true);
- return s;
- }
-
- /***********************************|****************************************/
-